﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class webadmin_pages_cosodaotao_frm : System.Web.UI.Page
{
    private Common _common;
    public static string _muc_chon = "0";
    public string act = "";
    public string mess = "";
    public string messnotID = "";
    protected void btn_choose_nn_Click(object sender, EventArgs e)
    {
        string str = _muc_chon;
        if (str != null)
        {
            if (!(str == "0"))
            {
                if (!(str == "1"))
                {
                    if (!(str == "2"))
                    {
                        if (str == "3")
                        {
                            this.ListBox_dm_nn.Items.Add(this.drop_nganhnghe_cap4.SelectedValue + "-" + this.drop_nganhnghe_cap4.SelectedItem.Text);
                        }
                        return;
                    }
                    this.ListBox_dm_nn.Items.Add(this.drop_nganhnghe_cap3.SelectedValue + "-" + this.drop_nganhnghe_cap3.SelectedItem.Text);
                    return;
                }
            }
            else
            {
                this.ListBox_dm_nn.Items.Add(this.drop_nganhnghe_cap1.SelectedValue + "-" + this.drop_nganhnghe_cap1.SelectedItem.Text);
                return;
            }
            this.ListBox_dm_nn.Items.Add(this.drop_nganhnghe_cap2.SelectedValue + "-" + this.drop_nganhnghe_cap2.SelectedItem.Text);
        }
    }

    protected void btn_del_item_choose_Click(object sender, EventArgs e)
    {
        this.ListBox_dm_nn.Items.RemoveAt(this.ListBox_dm_nn.SelectedIndex);
    }

    protected void btn_remove1_Click(object sender, EventArgs e)
    {
        this.drop_nganhnghe_cap2.Visible = false;
        this.btn_remove1.Visible = false;
        _muc_chon = "0";
    }

    protected void btn_remove2_Click(object sender, EventArgs e)
    {
        this.drop_nganhnghe_cap3.Visible = false;
        this.btn_remove2.Visible = false;
        this.btn_remove1.Visible = true;
        _muc_chon = "1";
    }

    protected void btn_remove3_Click(object sender, EventArgs e)
    {
        this.drop_nganhnghe_cap4.Visible = false;
        this.btn_remove3.Visible = false;
        this.btn_remove2.Visible = true;
        _muc_chon = "2";
    }

    protected void btn_save_Click(object sender, EventArgs e)
    {
        string str = "";
        for (int i = 0; i < this.ListBox_dm_nn.Items.Count; i++)
        {
            if (i < (this.ListBox_dm_nn.Items.Count - 1))
            {
                str = str + this.ListBox_dm_nn.Items[i].Text.Split(new char[] { '-' })[0] + ",";
            }
            else
            {
                str = str + this.ListBox_dm_nn.Items[i].Text.Split(new char[] { '-' })[0];
            }
        }
        SqlParameter[] sqlparam = new SqlParameter[15];
        sqlparam[1] = new SqlParameter("@ten_co_so_dt", this.txt_csdt_ten.Text);
        sqlparam[2] = new SqlParameter("@ma_mo_hinh_dt", this.drop_mohinhdaotao.SelectedValue);
        sqlparam[3] = new SqlParameter("@nam_thanh_lap", this.txt_namthanhlap.Text);
        sqlparam[4] = new SqlParameter("@nganh_dao_tao", str);
        sqlparam[5] = new SqlParameter("@ma_dia_chi", (this.drop_px.Items.Count > 0) ? this.drop_px.SelectedValue : this.drop_qh.SelectedValue);
        sqlparam[6] = new SqlParameter("@dia_chi", this.txt_csdt_diachi_ct.Text);
        sqlparam[7] = new SqlParameter("@dien_thoai", this.txt_csdt_dienthoai.Text);
        sqlparam[8] = new SqlParameter("@fax", this.txt_csdt_fax.Text);
        sqlparam[9] = new SqlParameter("@trang_chu", this.txt_csdt_web.Text);
        sqlparam[10] = new SqlParameter("@gioi_thieu", this.txt_csdt_gioithieu.Text);
        sqlparam[11] = new SqlParameter("@chi_tiet_khac", this.txt_csdt_ghichu.Text);
        sqlparam[12] = new SqlParameter("@he_thong", 1);
        sqlparam[13] = new SqlParameter("@ma_nguoi_dung", this.Session["UserID"]);
        sqlparam[14] = new SqlParameter("@kich_hoat", false);
        if (string.IsNullOrEmpty(base.Request.Params["id"]))
        {
            string str2 = this._common.execScalar("select (max(ma_co_so_dt) +1) as lastid from DaoTao_co_so_dao_tao");
            if (!string.IsNullOrEmpty(str2))
            {
                str2 = Convert.ToInt32(str2).ToString("D4");
            }
            else
            {
                str2 = "0001";
            }
            sqlparam[0] = new SqlParameter("@ma_co_so_dt", str2);
            if (this._common.ExeceuteByStoreProcedure(sqlparam, "COSODAOTAO_ADD") > 0)
            {
                base.Response.Redirect("cosodaotao_list.aspx?m=1");
            }
        }
        else
        {
            sqlparam[0] = new SqlParameter("@ma_co_so_dt", this.txtMa.Text);
            if (this._common.ExeceuteByStoreProcedure(sqlparam, "COSODAOTAO_UPDATE") > 0)
            {
                base.Response.Redirect("cosodaotao_list.aspx?m=1");
            }
        }
    }

    protected void drop_nganhnghe_cap1_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.LoadNganhkinhdoanh("1");
        _muc_chon = "1";
    }

    protected void drop_nganhnghe_cap2_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.LoadNganhkinhdoanh("2");
        _muc_chon = "2";
    }

    protected void drop_nganhnghe_cap3_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.LoadNganhkinhdoanh("3");
        _muc_chon = "3";
    }

    protected void drop_qh_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.drop_px.DataSource = this._common.ExeceuteDataset("select * from DanhMuc_dia_chi where ma_dia_chi_cha = '" + this.drop_qh.SelectedValue + "'");
        this.drop_px.DataTextField = "ten_dia_chi";
        this.drop_px.DataValueField = "ma_dia_chi";
        this.drop_px.DataBind();
    }

    protected void drop_tp_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.drop_qh.Items.Clear();
        this.drop_px.Items.Clear();
        DataSet set = this._common.ExeceuteDataset("select * from DanhMuc_dia_chi where ma_dia_chi_cha = '" + this.drop_tp.SelectedValue + "'");
        this.drop_qh.DataSource = set;
        this.drop_qh.DataTextField = "ten_dia_chi";
        this.drop_qh.DataValueField = "ma_dia_chi";
        this.drop_qh.DataBind();
        this.drop_px.DataSource = this._common.ExeceuteDataset("select * from DanhMuc_dia_chi where ma_dia_chi_cha = '" + set.Tables[0].Rows[0][0].ToString() + "'");
        this.drop_px.DataTextField = "ten_dia_chi";
        this.drop_px.DataValueField = "ma_dia_chi";
        this.drop_px.DataBind();
    }

    private void LoadDiachi()
    {
        DataSet set = this._common.ExeceuteDataset("select * from DanhMuc_dia_chi where muc = 0");
        this.drop_tp.DataSource = set;
        this.drop_tp.DataTextField = "ten_dia_chi";
        this.drop_tp.DataValueField = "ma_dia_chi";
        this.drop_tp.DataBind();
        DataSet set2 = this._common.ExeceuteDataset("select * from DanhMuc_dia_chi where ma_dia_chi_cha = '" + set.Tables[0].Rows[0][0].ToString() + "'");
        this.drop_qh.DataSource = set2;
        this.drop_qh.DataTextField = "ten_dia_chi";
        this.drop_qh.DataValueField = "ma_dia_chi";
        this.drop_qh.DataBind();
        this.drop_px.DataSource = this._common.ExeceuteDataset("select * from DanhMuc_dia_chi where ma_dia_chi_cha = '" + set2.Tables[0].Rows[0][0].ToString() + "'");
        this.drop_px.DataTextField = "ten_dia_chi";
        this.drop_px.DataValueField = "ma_dia_chi";
        this.drop_px.DataBind();
    }

    private void LoadDiachiDachon(string ma_dia_chi)
    {
        DataSet set = this._common.ExeceuteDataset("select * from DanhMuc_dia_chi where ma_dia_chi = '" + ma_dia_chi + "'");
        if (set.Tables[0].Rows[0]["muc"].ToString().Trim().Equals("2"))
        {
            this.drop_px.Items.Clear();
            this.drop_qh.Items.Clear();
            DataSet set2 = this._common.ExeceuteDataset("select * from DanhMuc_dia_chi where ma_dia_chi_cha = '" + set.Tables[0].Rows[0]["ma_dia_chi_cha"].ToString() + "'");
            this.drop_px.DataSource = set2;
            this.drop_px.DataTextField = "ten_dia_chi";
            this.drop_px.DataValueField = "ma_dia_chi";
            this.drop_px.DataBind();
            this.drop_px.Items.FindByValue(ma_dia_chi).Selected = true;
            DataSet set3 = this._common.ExeceuteDataset("select * from DanhMuc_dia_chi where ma_dia_chi_cha = '" + this._common.execScalar("select ma_dia_chi_cha from DanhMuc_dia_chi where ma_dia_chi = '" + set2.Tables[0].Rows[0]["ma_dia_chi_cha"].ToString() + "'") + "'");
            this.drop_qh.DataSource = set3;
            this.drop_qh.DataTextField = "ten_dia_chi";
            this.drop_qh.DataValueField = "ma_dia_chi";
            this.drop_qh.DataBind();
            this.drop_qh.Items.FindByValue(set.Tables[0].Rows[0]["ma_dia_chi_cha"].ToString()).Selected = true;
            this.drop_tp.Items.FindByValue(this._common.execScalar("select ma_dia_chi_cha from DanhMuc_dia_chi where ma_dia_chi = '" + set.Tables[0].Rows[0]["ma_dia_chi_cha"].ToString() + "'")).Selected = true;
        }
        if (set.Tables[0].Rows[0]["muc"].ToString().Trim().Equals("1"))
        {
            this.drop_px.Items.Clear();
            this.drop_qh.Items.Clear();
            DataSet set4 = this._common.ExeceuteDataset("select * from DanhMuc_dia_chi where ma_dia_chi_cha = '" + set.Tables[0].Rows[0]["ma_dia_chi_cha"].ToString() + "'");
            this.drop_qh.DataSource = set4;
            this.drop_qh.DataTextField = "ten_dia_chi";
            this.drop_qh.DataValueField = "ma_dia_chi";
            this.drop_qh.DataBind();
            this.drop_qh.Items.FindByValue(set.Tables[0].Rows[0]["ma_dia_chi"].ToString()).Selected = true;
            this.drop_tp.Items.FindByValue(set.Tables[0].Rows[0]["ma_dia_chi_cha"].ToString()).Selected = true;
        }
    }

    private void LoadInfo(string id)
    {
        DataSet set = this._common.ExeceuteDataset("select * from DaoTao_co_so_dao_tao as A left join DaoTao_mo_hinh_dao_tao as B on A.ma_mo_hinh_dt = B.ma_mo_hinh_dt where ma_co_so_dt = '" + id + "'");
        if (set.Tables[0].Rows.Count > 0)
        {
            this.txt_csdt_diachi_ct.Text = set.Tables[0].Rows[0]["dia_chi"].ToString();
            this.txt_csdt_dienthoai.Text = set.Tables[0].Rows[0]["dien_thoai"].ToString();
            this.txt_csdt_fax.Text = set.Tables[0].Rows[0]["fax"].ToString();
            this.txt_csdt_ghichu.Text = set.Tables[0].Rows[0]["chi_tiet_khac"].ToString();
            this.txt_csdt_gioithieu.Text = set.Tables[0].Rows[0]["gioi_thieu"].ToString();
            this.txt_csdt_ten.Text = set.Tables[0].Rows[0]["ten_co_so_dt"].ToString();
            this.txt_csdt_web.Text = set.Tables[0].Rows[0]["trang_chu"].ToString();
            this.txt_namthanhlap.Text = set.Tables[0].Rows[0]["nam_thanh_lap"].ToString();
            this.drop_mohinhdaotao.Items.FindByValue(set.Tables[0].Rows[0]["ma_mo_hinh_dt"].ToString()).Selected = true;
            this.LoadNganhngheDachon(set.Tables[0].Rows[0]["nganh_dao_tao"].ToString());
            this.LoadDiachiDachon(set.Tables[0].Rows[0]["ma_dia_chi"].ToString());
        }
    }

    private void LoadMohinh()
    {
        DataSet set = this._common.ExeceuteDataset("select * from DaoTao_mo_hinh_dao_tao");
        this.drop_mohinhdaotao.DataSource = set;
        this.drop_mohinhdaotao.DataTextField = "ten_mo_hinh_dt";
        this.drop_mohinhdaotao.DataValueField = "ma_mo_hinh_dt";
        this.drop_mohinhdaotao.DataBind();
    }

    private void LoadNganhkinhdoanh(string muc)
    {
        string sql = "select * from DanhMuc_nganh_kinh_doanh where muc = " + muc;
        DataSet set = this._common.ExeceuteDataset(sql);
        if (muc.Trim().Equals("0"))
        {
            this.drop_nganhnghe_cap1.Visible = true;
            this.drop_nganhnghe_cap1.DataSource = set;
            this.drop_nganhnghe_cap1.DataTextField = "ten_nganh_kd";
            this.drop_nganhnghe_cap1.DataValueField = "ma_nganh_kd";
            this.drop_nganhnghe_cap1.DataBind();
        }
        if (muc.Trim().Equals("1"))
        {
            this.drop_nganhnghe_cap2.Visible = true;
            this.drop_nganhnghe_cap2.DataSource = set;
            this.drop_nganhnghe_cap2.DataTextField = "ten_nganh_kd";
            this.drop_nganhnghe_cap2.DataValueField = "ma_nganh_kd";
            this.drop_nganhnghe_cap2.DataBind();
            this.btn_remove1.Visible = true;
            this.btn_remove2.Visible = false;
            this.btn_remove3.Visible = false;
        }
        if (muc.Trim().Equals("2"))
        {
            this.drop_nganhnghe_cap3.Visible = true;
            this.drop_nganhnghe_cap3.DataSource = set;
            this.drop_nganhnghe_cap3.DataTextField = "ten_nganh_kd";
            this.drop_nganhnghe_cap3.DataValueField = "ma_nganh_kd";
            this.drop_nganhnghe_cap3.DataBind();
            this.btn_remove1.Visible = false;
            this.btn_remove2.Visible = true;
            this.btn_remove3.Visible = false;
        }
        if (muc.Trim().Equals("3"))
        {
            this.drop_nganhnghe_cap4.Visible = true;
            this.drop_nganhnghe_cap4.DataSource = set;
            this.drop_nganhnghe_cap4.DataTextField = "ten_nganh_kd";
            this.drop_nganhnghe_cap4.DataValueField = "ma_nganh_kd";
            this.drop_nganhnghe_cap4.DataBind();
            this.btn_remove1.Visible = false;
            this.btn_remove2.Visible = false;
            this.btn_remove3.Visible = true;
        }
    }

    private void LoadNganhngheDachon(string nn)
    {
        string str = "";
        string[] strArray = nn.Split(new char[] { ',' });
        for (int i = 0; i < strArray.Length; i++)
        {
            if (i < (strArray.Length - 1))
            {
                str = str + "'" + strArray[i] + "',";
            }
            else
            {
                str = str + "'" + strArray[i] + "'";
            }
        }
        DataSet set = this._common.ExeceuteDataset("select * from DanhMuc_nganh_kinh_doanh where ma_nganh_kd IN (" + str + ")");
        if (set.Tables[0].Rows.Count > 0)
        {
            for (int j = 0; j < set.Tables[0].Rows.Count; j++)
            {
                this.ListBox_dm_nn.Items.Add(set.Tables[0].Rows[j]["ma_nganh_kd"].ToString() + "-" + set.Tables[0].Rows[j]["ten_nganh_kd"].ToString());
            }
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            this._common = new Common();
            if (!base.IsPostBack)
            {
                this.LoadMohinh();
                this.LoadNganhkinhdoanh("0");
                this.LoadDiachi();
                if (!string.IsNullOrEmpty(base.Request.Params["id"]))
                {
                    this.txtMa.Text = this._common.TextFormat(base.Request.Params["id"].ToString());
                    this.LoadInfo(this._common.TextFormat(base.Request.Params["id"].ToString()));
                }
            }
        }
        catch (Exception exception)
        {
            this.mess = "Lỗi hệ thống - " + exception.Message;
        }
    }
}
